package com.huarun.module.video.controller;

import com.alibaba.fastjson.JSONObject;
import com.huarun.common.util.Constant;
import com.huarun.common.util.http.HttpParamers;
import com.huarun.common.util.http.HttpService;
import com.huarun.module.webapi.WelingService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping({"/wx/video"})
@Slf4j
public class VideoController {

    @Autowired
    private WelingService welingService;

    @ApiOperation(value = "获取HLS流播放地址", notes = "HLS流播放地址")
    @RequestMapping(value = {"/getPlayUrl"}, method = {org.springframework.web.bind.annotation.RequestMethod.GET})
    @ResponseBody
    public Object getPlayUrl() throws Exception {
        String host = Constant.WELINK_LOGIN_HOST;
        String url = "/common/video/getPlayUrl";
        HttpParamers paramers = HttpParamers.httpGetParamers();
        String[] tokenTicketArray = welingService.getTokenAndIotimTicket();
        paramers.addParam("token", tokenTicketArray[0]);
        paramers.addParam("iotim_ticket", tokenTicketArray[1]);
        paramers.addParam("din", Constant.WELINK_APP_VIDEO_DIN);//设备的唯一标识
        HttpService httpService = new HttpService(host);
        String videoReturn = httpService.service(url, paramers);
        JSONObject jsonReturn = JSONObject.parseObject(videoReturn);
        log.info("HLS流播放地址数据: " + jsonReturn.toJSONString());
        return jsonReturn;
    }
}
